package gitee.derumin.lifeissohard.mixin;

import com.mojang.blaze3d.systems.RenderSystem;
import gitee.derumin.lifeissohard.lishhungersystem.LishHungerManager;
import gitee.derumin.lifeissohard.tools.LishIdentifiers;
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.invoke.arg.Args;


@Mixin(InGameHud.class)
public abstract class InGameHudMixin extends DrawableHelper {

    @Shadow
    private int scaledWidth;
    @Shadow
    private int scaledHeight;
    @Shadow
    private int ticks;

    @Shadow
    private PlayerEntity getCameraPlayer(){
        return null;
    }


    @Inject(method = "renderStatusBars" , at = @At(value = "CONSTANT" , args = "stringValue=food" , shift = At.Shift.AFTER))
    private void renderStomachContains(MatrixStack matrices, CallbackInfo ci){
        PlayerEntity player = this.getCameraPlayer();
        if (player != null && player.getHungerManager() instanceof LishHungerManager lishHungerManager) {
            RenderSystem.setShaderTexture(0, LishIdentifiers.Textures.Gui.ICONS);
            int ltx = this.scaledWidth / 2 + 91;
            int lty = this.scaledHeight - 48;
            int containsH = Math.max(0,(int)(18*lishHungerManager.getStomachContains()/1000000d));
            if (containsH <= 18){
                this.drawTexture(matrices, ltx, lty, 0, 0, 9, 18);
                this.drawTexture(matrices, ltx, lty+18-containsH, 9, 18-containsH, 9, containsH);
            }else {
                if (this.ticks % Math.max(2 , 38-containsH) == 0){
                    ltx += (int) (5*Math.random())-2;
                    lty += (int) (5*Math.random())-2;
                }
                this.drawTexture(matrices, ltx, lty, 18, 0, 10, 18);
            }
            ltx = this.scaledWidth / 2 + 82;
            lty = this.scaledHeight - 48;
            int waterState = (int) (lishHungerManager.getWaterPercent()/0.6d)-80;
            boolean shouldJump = (waterState <= 10 && this.ticks % Math.max(2,2*waterState) == 0) || (waterState >= 26 && this.ticks % Math.max(2,4*(31-waterState)-2) == 0);
            for (int i = 0;i <10; i++){
                int x = ltx - 8 * i;
                int y = lty;
                if (shouldJump){
                    x += (int) (3*Math.random())-1;
                    y += (int) (3*Math.random())-1;
                }
                this.drawTexture(matrices, x, y, 28, 0, 9, 9);
                if (i < waterState - 20){
                    this.drawTexture(matrices, x, y, 55, 0, 9, 9);
                } else if (i < waterState - 10){
                    this.drawTexture(matrices, x, y, 46, 0, 9, 9);
                } else if (i < waterState){
                    this.drawTexture(matrices, x, y, 37, 0, 9, 9);
                }

            }
            RenderSystem.setShaderTexture(0, Screen.GUI_ICONS_TEXTURE);
        }
    }

    @ModifyArgs(method = "renderStatusBars" , at = @At(value = "INVOKE" , target = "Lnet/minecraft/client/gui/hud/InGameHud;drawTexture(Lnet/minecraft/client/util/math/MatrixStack;IIIIII)V") , slice = @Slice(from = @At(value = "INVOKE" , target = "Lnet/minecraft/util/math/random/Random;nextInt(I)I")))
    private void modifyFoodAndPops(Args args){
        if (args.get(4).equals(27) && this.getCameraPlayer() != null && this.getCameraPlayer().getHungerManager() instanceof LishHungerManager lishHungerManager){
            int lty = this.scaledHeight - 39;
            if (lishHungerManager.getStarveLevel() > 0f && this.ticks % (int)Math.max(2,20d-20d*lishHungerManager.getStarveLevel()) == 0){
                int ltx = args.get(1);
                args.set(1,ltx + (int)(3*Math.random())-1);
                args.set(2,lty + (int)(3*Math.random())-1);
            }else args.set(2,lty);
        }else {
            int lty = args.get(2);
            args.set(2,lty -9);
        }
    }
}
